package com.qlmy.cn.config

import com.qlmy.cn.api.ApiHelper
import com.qlmy.cn.utils.SPUtils
import com.wbs.shop.nuby.BuildConfig

object ServiceConfig {
    var API: String = ""
    var H5: String? = null
    var TOTAL_SERVICE_TAG: String? = null
    var API_SERVICE_TAG: String? = null
    var H5_SERVICE_TAG: String? = null
    private val SERVICE_CONFIGS: MutableList<ServiceConfigBean> = mutableListOf()
    private var CURRENT_SERVICE_CONFIG: CurrentServiceConfig? = null
    private val RELEASE = ServiceConfigBean(
            1,
            "正式",
            "release",
            "pro",
            "http://m.wemei.vip",
            "http://m.wemei.vip"
    )
    private val UAT = ServiceConfigBean(
            2,
            "外网测试",
            "uat",
            "test",
            "http://m.wemei.vip",
            "http://m.wemei.vip"
    )
    private val TEST = ServiceConfigBean(
            3,
            "本地测试",
            "debug",
            "test",
            "http://m.wemei.vip",
            "http://m.wemei.vip"
    )

    init {
        val declaredFields = ServiceConfig::class.java.declaredFields
        for (declaredField in declaredFields) {
            if (declaredField.type == ServiceConfigBean::class.java) {
                try {
                    SERVICE_CONFIGS.add(declaredField[this] as ServiceConfigBean)
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        }
        SERVICE_CONFIGS.sortWith { o1: ServiceConfigBean, o2: ServiceConfigBean -> o1.order - o2.order }
    }

    fun reset() {
        CURRENT_SERVICE_CONFIG = null
        ApiHelper.reset()
    }

    fun saveLocal(apiUrl: String) {
        SPUtils.edit {
            putBoolean(SP_KEY_SAVE_LOCA, true)
            putString(SP_KEY_API, apiUrl)
        }
        reset()
    }

    fun getCurrentServiceConfig(): CurrentServiceConfig {
        if (CURRENT_SERVICE_CONFIG == null) {
            val saveLoca: Boolean = SPUtils.getBoolean(SP_KEY_SAVE_LOCA, false)
            val defaultServiceConfig: ServiceConfigBean = getDefaultServiceConfig()
            val currentServiceConfig: CurrentServiceConfig
            if (saveLoca) {
                currentServiceConfig = CurrentServiceConfig()
                currentServiceConfig.pushTag = defaultServiceConfig.pushTag
                currentServiceConfig.apiUrl = SPUtils.getString(SP_KEY_API)
                currentServiceConfig.h5Url = SPUtils.getString(SP_KEY_H5)
                currentServiceConfig.totalServiceTag = SPUtils.getString(SP_KEY_TOTAL_SERVICE_TAG)
                currentServiceConfig.apiServiceTag = SPUtils.getString(SP_KEY_API_SERVICE_TAG)
                currentServiceConfig.h5ServiceTag = SPUtils.getString(SP_KEY_h5_SERVICE_TAG)
                return currentServiceConfig
            } else {
                currentServiceConfig = CurrentServiceConfig()
                currentServiceConfig.pushTag = defaultServiceConfig.pushTag
                currentServiceConfig.apiUrl = if (API.isEmpty()) defaultServiceConfig.apiUrl else API
                currentServiceConfig.h5Url = if (H5 == null) defaultServiceConfig.h5Url else H5
                currentServiceConfig.totalServiceTag = if (TOTAL_SERVICE_TAG == null) defaultServiceConfig.serviceTag else TOTAL_SERVICE_TAG
                currentServiceConfig.apiServiceTag = if (API_SERVICE_TAG == null) defaultServiceConfig.serviceTag else API_SERVICE_TAG
                currentServiceConfig.h5ServiceTag = if (H5_SERVICE_TAG == null) defaultServiceConfig.serviceTag else H5_SERVICE_TAG
            }
            CURRENT_SERVICE_CONFIG = currentServiceConfig
        }
        return CURRENT_SERVICE_CONFIG!!
    }

    private fun getDefaultServiceConfig(): ServiceConfigBean {
        var defaultServiceConfig: ServiceConfigBean? = null
        for (serviceConfig in SERVICE_CONFIGS) {
            if (serviceConfig.serviceTag == BuildConfig.BUILD_TYPE) defaultServiceConfig = serviceConfig
        }
        if (defaultServiceConfig == null) defaultServiceConfig = TEST
        return defaultServiceConfig
    }

    fun getServiceConfigs(): MutableList<ServiceConfigBean> {
        val serviceConfigs: MutableList<ServiceConfigBean> = mutableListOf()
        for (serviceConfig in SERVICE_CONFIGS) {
            serviceConfigs.add(serviceConfig.copy())
        }
        return serviceConfigs
    }
}

open class ServiceConfigBean(
        var order: Int = 0,
        var serviceName: String? = null,
        var serviceTag: String? = null,
        var pushTag: String? = null,
        var h5Url: String? = null,
        var apiUrl: String = "",
        var check: Boolean = false
) {
    open fun copy(): ServiceConfigBean = ServiceConfigBean(order, serviceName, serviceTag, pushTag, h5Url, apiUrl)
}

class CurrentServiceConfig(
        var totalServiceTag: String? = null,
        var apiServiceTag: String? = null,
        var h5ServiceTag: String? = null
) : ServiceConfigBean()